package com.byd.android.VideoPlayer;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import com.byd.android.agreement.message.Video;

import java.util.ArrayList;
import java.util.List;

public class VideoProvider implements AbstructProvider {
    private Context context;

    public VideoProvider(Context context) {
        this.context = context;
    }
    public VideoProvider(){}
    @Override
    public List<Video> getList() {
        List<Video> list = null;
        if (context != null) {
            Cursor cursor = context.getContentResolver().query(
                    MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null,
                    null, null);
            if (cursor != null) {
                list = new ArrayList<>();
                int id = -1;
                while (cursor.moveToNext()) {
                    id++;
                    String videoName = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
                    String artist = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST));
                    String path = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
                    int duration = cursor
                            .getInt(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
                    long size = cursor
                            .getLong(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
                    Video video = new Video(id, duration, artist, videoName, path) ;
                    list.add(video);
                }
                cursor.close();
            }
        }
        return list;
    }
    /**
     * 把long格式的时间转化成00:00:00
     */
    public static String millions2hour(int million) {
        int hour = million / (60 * 60 * 1000);
        int minute = (million - hour * 60 * 60 * 1000) / (60 * 1000);
        int seconds = (million - hour * 60 * 60 * 1000 - minute * 60 * 1000) / 1000;

        if (seconds >= 60) {
            seconds = seconds % 60;
            minute += seconds / 60;
        }
        if (minute >= 60) {
            minute = minute % 60;
            hour += minute / 60;
        }

        String sh = "";
        String sm = "";
        String ss = "";
        if (hour < 10) {
            sh = "0" + hour;
        } else {
            sh = String.valueOf(hour);
        }
        if (minute < 10) {
            sm = "0" + minute;
        } else {
            sm = String.valueOf(minute);
        }
        if (seconds < 10) {
            ss = "0" + seconds;
        } else {
            ss = String.valueOf(seconds);
        }

        return sh + ":" + sm + ":" + ss;
    }

}
